package com.studyweb.controller;

import com.studyweb.pojo.entity.CommonSenseAccumulation;
import com.studyweb.pojo.vo.Result;
import com.studyweb.service.CommonSenseAccumulationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/common-sense-accumulation")
@CrossOrigin
public class CommonSenseAccumulationController {
    
    @Autowired
    private CommonSenseAccumulationService commonSenseAccumulationService;
    
    /**
     * 获取所有常识积累列表
     * @return 常识积累列表
     */
    @GetMapping("/list")
    public Result<List<CommonSenseAccumulation>> list() {
        try {
            List<CommonSenseAccumulation> list = commonSenseAccumulationService.list();
            return Result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取常识积累列表失败");
        }
    }
    
    /**
     * 根据ID获取常识积累详情
     * @param id 主键ID
     * @return 常识积累详情
     */
    @GetMapping("/{id}")
    public Result<CommonSenseAccumulation> getById(@PathVariable Long id) {
        try {
            CommonSenseAccumulation commonSenseAccumulation = commonSenseAccumulationService.getById(id);
            if (commonSenseAccumulation != null) {
                return Result.success(commonSenseAccumulation);
            } else {
                return Result.error("未找到对应的常识积累内容");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取常识积累详情失败");
        }
    }
    
    /**
     * 新增常识积累
     * @param commonSenseAccumulation 常识积累对象
     * @return 操作结果
     */
    @PostMapping("/add")
    public Result<String> add(@RequestBody CommonSenseAccumulation commonSenseAccumulation) {
        try {
            commonSenseAccumulationService.add(commonSenseAccumulation);
            return Result.success("新增常识积累成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("新增常识积累失败");
        }
    }
    
    /**
     * 更新常识积累
     * @param commonSenseAccumulation 常识积累对象
     * @return 操作结果
     */
    @PutMapping("/update")
    public Result<String> update(@RequestBody CommonSenseAccumulation commonSenseAccumulation) {
        try {
            commonSenseAccumulationService.update(commonSenseAccumulation);
            return Result.success("更新常识积累成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新常识积累失败");
        }
    }
    
    /**
     * 删除常识积累
     * @param id 主键ID
     * @return 操作结果
     */
    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Long id) {
        try {
            commonSenseAccumulationService.delete(id);
            return Result.success("删除常识积累成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除常识积累失败");
        }
    }
    
    /**
     * 批量删除常识积累
     * @param ids 主键ID列表
     * @return 操作结果
     */
    @DeleteMapping("/delete/batch")
    public Result<String> batchDelete(@RequestBody List<Long> ids) {
        try {
            commonSenseAccumulationService.batchDelete(ids);
            return Result.success("批量删除常识积累成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("批量删除常识积累失败");
        }
    }
    
    /**
     * 搜索常识积累
     * @param keyword 关键词
     * @return 搜索结果
     */
    @GetMapping("/search")
    public Result<List<CommonSenseAccumulation>> search(@RequestParam String keyword) {
        try {
            List<CommonSenseAccumulation> list = commonSenseAccumulationService.search(keyword);
            return Result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("搜索常识积累失败");
        }
    }
}